package com.tfy.liquor.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tfy.liquor.bean.RolloverRecords;
import com.tfy.liquor.dao.RolloverRecordsDao;
import com.tfy.liquor.service.RolloverRecordsService;
import com.tfy.liquor.util.MyStringUtil;
import com.tfy.liquor.util.poi.ExportExcelUtil;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class RolloverRecordsServiceImpl implements RolloverRecordsService {
    @Autowired
    private RolloverRecordsDao rolloverRecordsDao;

    @Override
    public Boolean addRolloverRecords(Workbook workbook) {
        //开始解析excel
        Sheet sheet = workbook.getSheetAt(0);     //读取sheet 0
        int firstRowIndex = sheet.getFirstRowNum() + 1;   //第一行是列名，所以不读
        int lastRowIndex = sheet.getLastRowNum();

        for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
            Row row = sheet.getRow(rIndex);
            if (row != null) {
                String id = MyStringUtil.getValue(row.getCell(0));
                // 判断表格数据是否合法
                if (null == id || "".equals(id.trim()) || " ".equals(id)) {
                    continue;
                }
                // 取出数据
                RolloverRecords rolloverRecords = new RolloverRecords();
                rolloverRecords.setId(id); // id
                rolloverRecords.setUserId(MyStringUtil.getValue(row.getCell(1))); // userId
                rolloverRecords.setGiveId(MyStringUtil.getValue(row.getCell(2))); // giveId
                rolloverRecords.setOrderId(MyStringUtil.getValue(row.getCell(3))); // orderId
                rolloverRecords.setAmount(new Double(String.valueOf(row.getCell(4)))); // amount
                rolloverRecords.setBalance(new Double(String.valueOf(row.getCell(5)))); // balance
                rolloverRecords.setCategory(String.valueOf(row.getCell(6))); // category
                rolloverRecords.setIllustrate(String.valueOf(row.getCell(7))); // illustrate
                // 时间类型特殊处理
                Cell cell = row.getCell(8);
                rolloverRecords.setCreateDate(new Date(String.valueOf(cell.getDateCellValue()))); // createDate
                // 批量插入数据
                System.err.println(rolloverRecords);
                // 如果数据中存在，则跳过
                if (rolloverRecordsDao.getRepeat(rolloverRecords.getId()) > 0) {
                    continue;
                }
                if (rolloverRecordsDao.addRolloverRecords(rolloverRecords) < 0) {
                    return false;
                }
            }
        }
        return true;
    }

    // 全文检索数据
    @Override
    public Model getInfo(Model model, Integer pageNum, Integer pageSize, String value, String userId, String cap, String date) {
        // 如果参数为空，初始化参数
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 15;
        }
        // 如果传入的参数是负数，则默认为1
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 ? 15 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<RolloverRecords> list;
        // 如果所有参数都为空
        if ((null == value || "".equals(value.trim())) && (null == userId || "".equals(userId.trim())) && (null == cap || "".equals(cap.trim())) && (null == date || "".equals(date.trim()))) {
            list = rolloverRecordsDao.getRolloverRecords();
        } else if ((null == value || "".equals(value.trim())) && (null == date || "".equals(date.trim()))) {
            if (null == cap || "".equals(cap.trim())) { // 如果是封顶单
                list = rolloverRecordsDao.getRolloverRecordsByUserId(userId);
            } else {
                list = rolloverRecordsDao.getRolloverByUserId(userId);
            }
        } else if (null != date && !"".equals(date.trim())) { // 根据日期筛选
            if ((null == cap || "".equals(cap.trim())) && (null == userId || "".equals(userId.trim()))) {
                list = rolloverRecordsDao.getRolloverByDate(date);
            } else if (null == userId || "".equals(userId.trim())) {
                list = rolloverRecordsDao.getOrderByDate(date);
            } else {
                list = rolloverRecordsDao.getOrderByUserIdDate(userId, date);
            }
        } else {
            list = rolloverRecordsDao.getInfo(value);
        }
        // 可以将结果转换为 Page , 然后获取 count 和其他结果值
        Page page = (Page) list;
        model.addAttribute("page", page);
        model.addAttribute("list", list);
        return model;
    }

    // 根据id查询数据
    @Override
    public ArrayList<RolloverRecords> getRolloverRecordsByUserId(String userId) {
        return rolloverRecordsDao.getRolloverRecords();
    }

    // 根据日期查询数据
    @Override
    public ArrayList<RolloverRecords> getOrderByDate(String date) {
        return rolloverRecordsDao.getOrderByDate(date);
    }

    // 导出数据
    @Override
    public void ExportRolloverRecordsExcel(HttpServletResponse response, String value, String userId, String cap, String date) {
        ArrayList<RolloverRecords> list;
        // 如果所有参数都为空
        if ((null == value || "".equals(value.trim())) && (null == userId || "".equals(userId.trim())) && (null == cap || "".equals(cap.trim())) && (null == date || "".equals(date.trim()))) {
            list = rolloverRecordsDao.getRolloverRecords();
        } else if ((null == value || "".equals(value.trim())) && (null == date || "".equals(date.trim()))) {
            if (null == cap || "".equals(cap.trim())) { // 如果是封顶单
                list = rolloverRecordsDao.getRolloverRecordsByUserId(userId);
            } else {
                list = rolloverRecordsDao.getRolloverByUserId(userId);
            }
        } else if (null != date && !"".equals(date.trim())) { // 根据日期筛选
            if ((null == cap || "".equals(cap.trim())) && (null == userId || "".equals(userId.trim()))) {
                list = rolloverRecordsDao.getRolloverByDate(date);
            } else if (null == userId || "".equals(userId.trim())) {
                list = rolloverRecordsDao.getOrderByDate(date);
            } else {
                list = rolloverRecordsDao.getOrderByUserIdDate(userId, date);
            }
        } else {
            list = rolloverRecordsDao.getInfo(value);
        }
        if (null != list && list.size() > 0) {
            // 列名称
            Map<String, String> map = new LinkedHashMap<>();
            map.put("id", "编号");
            map.put("userId", "用户id");
            map.put("giveId", "赠送id");
            map.put("orderId", "订单编号");
            map.put("amount", "金额");
            map.put("balance", "余额");
            map.put("category", "类别");
            map.put("illustrate", "说明");
            map.put("createDate", "时间");
            map.put("remark", "备注");

            // 导出数据
            ExportExcelUtil.exportExcel(response, map, list, "订单转购物券信息", "rolloverRecords");
        }
    }
}
